package ModelLayer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

/**
 * Write a description of class FileLease here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class FileLease
{
    private File file;
    private BufferedWriter bw;
    private FileWriter fw;
    private BufferedReader br;
    private FileReader fr;
    private ArrayList<Lease> old_leases;

    /**
     * Constructor for objects of class FileLease
     */
    public FileLease()
    {
       file = new File("Lease.txt");
       if(!(file.exists()))
       {
           try
           {
               file.createNewFile();
           }
           
           catch (IOException e)
           {
               
           }
       }
       old_leases = get_data();
    }

    public boolean save_changes(ArrayList<Lease> new_leases)
    {
        if(has_changed(new_leases))
        {            
            try
            {
                fw = new FileWriter(file);
                bw = new BufferedWriter(fw);
                for(Lease lease : new_leases)
                {
                    String line = lease.give_string_data();
                    bw.write(line);
                    bw.newLine();
                }
                bw.close();
                old_leases = get_data();
                return true;
            }
            catch (IOException e)
            {
            }
        }
        return false;
    }
    
    private boolean has_changed(ArrayList<Lease> new_leases)
    {
        int size;
        if((size = old_leases.size()) == new_leases.size())
        {
            for(int i = 0; i < size; i++)
            {
                if(!(old_leases.get(i).give_string_data().equals(new_leases.get(i).give_string_data())))
                {
                    return true;
                }
            }
            return false;
        }
        return true;
    }
    
    public ArrayList<Lease> get_data()
    {
        ArrayList<Lease> leases_from_file = new ArrayList<>();
        String line;
        try
        {
            fr = new FileReader(file);
            try
            {
                br = new BufferedReader(fr);
                while ((line = br.readLine()) != null)
                {
                    if(line != "")
                    {
                        leases_from_file.add(retrive_leases(line));
                    }
                }
                br.close();
            }
            catch (IOException e)
            {
            }
        }
        catch (FileNotFoundException ex)
        {
        }
        return leases_from_file;
    }
    
    private Lease retrive_leases(String line)
    {
        String[] data = line.split("\t");
        double total_price = Double.parseDouble(data[3]);
        boolean is_closed = Boolean.parseBoolean(data[5]);
        return new Lease(data[0], data[1], data[2], total_price, make_array(data[4]), is_closed);
    } 
    
    private ArrayList<String> make_array(String ids)
    {
        ArrayList<String> returner = new ArrayList<>();
        String[] data = ids.split(" ");
        for(String id : data)
        {
            returner.add(id);
        }
        return returner;
    }
        
    

}
